#include "io.h"
#include "../aerolineas/aerolineas.h"
#include "../commons/errors.h"

void freeAerolinea(aerolinea_t aerolinea)
{
	int i, j;
	if(aerolinea == NULL)	return;
	if(aerolinea->aviones != NULL){	
		for(i=0 ; i < aerolinea->avionesCant; i++){
			if((aerolinea->aviones[i]) != NULL){
				for(j=0 ; j < (aerolinea->aviones[i])->drogasCant; j++){
					if(aerolinea->aviones[i]->drogas != NULL){
						free(((aerolinea->aviones[i])->drogas[j])->nombre);
						free((aerolinea->aviones[i])->drogas[j]);
					}
				}
				free((aerolinea->aviones[i])->drogas);
				free(aerolinea->aviones[i]);			
			}
		}

		free(aerolinea->aviones);
	}
        free(aerolinea);
}


int allocAviones(aerolinea_t aerolinea, int avionesCant)
{
        aerolinea->aviones = (avion_t *)malloc(avionesCant * sizeof(struct st_avion_t));
        if(aerolinea->aviones == NULL) return MEMORY_ERROR;
        
        return NO_ERRORS;
}

int allocDrogasAvion(avion_t avion, int drogasCant)
{	
        if(drogasCant == 1){
                    avion->drogas = (droga_t *)malloc(drogasCant * sizeof(struct st_droga_t));
        }else{
            avion->drogas = (droga_t *)realloc(avion->drogas, drogasCant * sizeof(struct st_droga_t));
        }
        if(avion->drogas == NULL) return MEMORY_ERROR;

	return NO_ERRORS;
}

int loadAerolinea(aerolinea_t aerolinea, char * fileName)
{
        int avionesCant;
        char nombre[MAX_NAME];
        int drogaCant;
        char drogaName[MAX_NAME];
        
        FILE * file;

        file = fopen(fileName,"r");
        
        if(file == NULL) 
                return ERR_FILE;
        
        /*Cargo Cant de aviones*/
        
        if((fscanf(file, "%d\n" ,&avionesCant) == 1) && (avionesCant > 0)/* && (avionesCant < MAX_AVION)*/){
        
            aerolinea->avionesCant=avionesCant;
            if(allocAviones(aerolinea, avionesCant) != NO_ERRORS) return MEMORY_ERROR;
            
        }else{
         
            return ERR_FILE;
            
        }
        
        int i;
	int ret;
	/*Cargo los aviones*/
	for( i = 0 ; i < avionesCant ; i++){
	  
	  avion_t avion = (avion_t) malloc(sizeof(struct st_avion_t));
	  if(avion == NULL) return MEMORY_ERROR;
	  
	  
	  ret = fscanf(file, "%s\n" , (char *)&nombre );
	  if( ret  != 1 ) return ERR_FILE;
	  
	 /*Cargo Origen del avion*/

	nombre[strlen(nombre)]='\0';
	int id =  get_city_id(nombre);
	if( id != -1){
		avion->ciudadDestinoId=id;
	}else {
		return ERR_FILE;
	}


	  
	 /*Cargo las drogas del avion*/ 
	 int cantDrogas=0;
	 
	 fpos_t posicion;
 	 fgetpos(file, &posicion);
	  while(fscanf(file, "%s %d\n" , drogaName, &drogaCant) == 2){
	      fgetpos(file, &posicion);
	  
	      droga_t droga = (droga_t) malloc(sizeof(struct st_droga_t));
	      if(droga == NULL) return MEMORY_ERROR;
	      
	      droga->nombre = (char *) malloc(strlen(drogaName)+1);
	      if(droga->nombre == NULL) return MEMORY_ERROR;
	      
	      strcpy(droga->nombre, drogaName);
	      droga->cant=drogaCant;
	      
	      cantDrogas++;
	      avion->drogasCant=cantDrogas;
	      if(allocDrogasAvion(avion, cantDrogas) != NO_ERRORS) return MEMORY_ERROR;	      	      
	      avion->drogas[cantDrogas-1]=droga;
	    
	  }
	  
 	 fsetpos(file, &posicion);
	 aerolinea->aviones[i] = avion; 
	  	  
	}
	
	fclose(file);
	      
	return NO_ERRORS;	
	
}
